package app.mvp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

/**
 * Created by 刘智强 on 2017/4/27.
 */

public class PwdEditText extends android.support.v7.widget.AppCompatEditText {

    /**
     *间隔距离
     *
     */
    private final int PWD_DISTENCE=1;


    /**
     *
     *密码大小
     *
     */
    private final int PWD_SIZE=5;

    /***
     *密码框长度
     *
     */
    private final int PWD_RECT_LENGTH=6;

    /**
     * 上下文
     */
    private Context context;

    /**
     *整个密码框的宽度
     *
     */
    private int mWidth;

    /**
     *
     *整个密码框的长度
     */
    private int mHeight;

    /*
    密码框
     */
    private Rect mRect;

    /*
    密码画笔
     */
    private Paint PwdPaint;

    /*
    密码框画笔
     */
    private Paint PwdRectPaint;

    /*
    输入密码长度
     */
    private int InputLength;

    /*
    输入完成监听
     */
    private OnInputFinishListener mOnInputFinishListener;


    /*
    构造方法
     */
    public PwdEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        //初始化密码画笔
        PwdPaint = new Paint();
        PwdPaint.setColor(Color.BLACK);
        PwdPaint.setAntiAlias(true);//抗锯齿
        PwdPaint.setStyle(Paint.Style.FILL);

        //初始化密码框画笔
        PwdRectPaint = new Paint();
        PwdRectPaint.setColor(Color.LTGRAY);
        PwdRectPaint.setStyle(Paint.Style.STROKE);
        PwdRectPaint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getWidth();
        mHeight =getHeight();

        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        canvas.drawRect(0,0,mWidth,mHeight,paint);

        //计算每个密码框的宽度
        int rectWidth = (mWidth-(PWD_RECT_LENGTH-1)*PWD_DISTENCE)/PWD_RECT_LENGTH;

        //绘制密码框
        for (int i= 0;i<PWD_RECT_LENGTH;i++)
        {
            int Left = (rectWidth+PWD_DISTENCE)*i;
            int Top =1;
            int Right = rectWidth+Left;
            int Bottom = mHeight-Top;
            mRect = new Rect(Left,Top,Right,Bottom);
            canvas.drawRect(mRect,PwdRectPaint);
        }

        //绘制密码
        for (int i=0;i<InputLength;i++)
        {
            int cx = rectWidth/2+(rectWidth+PWD_DISTENCE)*i;
            int cy = mHeight/2;
            canvas.drawCircle(cx,cy,PWD_SIZE,PwdPaint);
        }
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        this.InputLength = text.toString().length();
        invalidate();//隐藏窗体
        if (InputLength==PWD_RECT_LENGTH&&mOnInputFinishListener!=null)
        {
            mOnInputFinishListener.onInputFinish(text.toString());
        }
    }

    public interface OnInputFinishListener{
        /**
         *
         * 密码输入监听结束
         */
        void onInputFinish(String password);
    }

    /**
     *
     * 设置输入完成监听
     */
    public void setOnInputFinishListener(OnInputFinishListener onInputFinishListener)
    {
        this.mOnInputFinishListener =onInputFinishListener;
    }
}
